home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / ipip.c < prev    next >
C/C++ Source or Header  |  1992-05-14  |  4KB  |  193 lines

  1. /* @(#) $Header: ipip.c,v 1.2 92/05/14 13:20:10 deyke Exp $ */
  2.  
  3. #include "global.h"
  4.  
  5. #include <sys/types.h>
  6.  
  7. #include <netinet/in.h>
  8. #include <netinet/in_systm.h>
  9. #include <netinet/ip.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/socket.h>
  14.  
  15. extern char *sys_errlist[];
  16. extern int errno;
  17.  
  18. #include "mbuf.h"
  19. #include "iface.h"
  20. #include "timer.h"
  21. #include "internet.h"
  22. #include "netuser.h"
  23. #include "socket.h"
  24. #include "trace.h"
  25. #include "pktdrvr.h"
  26. #include "cmdparse.h"
  27. #include "hpux.h"
  28.  
  29. #define MAX_FRAME       2048
  30.  
  31. struct edv_t {
  32.   int type;
  33. #define IP_OVER_IP      0
  34. #define IP_OVER_UDP     1
  35.   int fd;
  36.   struct sockaddr_in addr;
  37. };
  38.  
  39. static int ipip_send __ARGS((struct mbuf *data, struct iface *ifp, int32 gateway, int prec, int del, int tput, int rel));
  40. static void ipip_recv __ARGS((void *argp));
  41.  
  42. /*---------------------------------------------------------------------------*/
  43.  
  44. static int ipip_send(data, ifp, gateway, prec, del, tput, rel)
  45. struct mbuf *data;
  46. struct iface *ifp;
  47. int32 gateway;
  48. int prec;
  49. int del;
  50. int tput;
  51. int rel;
  52. {
  53.  
  54.   char buf[MAX_FRAME];
  55.   int l;
  56.   struct edv_t *edv;
  57.  
  58.   dump(ifp, IF_TRACE_OUT, ifp->type, data);
  59.   ifp->rawsndcnt++;
  60.   ifp->lastsent = secclock();
  61.  
  62.   if (ifp->trace & IF_TRACE_RAW)
  63.     raw_dump(ifp, -1, data);
  64.  
  65.   l = pullup(&data, buf, sizeof(buf));
  66.   if (l <= 0 || data) {
  67.     free_p(data);
  68.     return (-1);
  69.   }
  70.  
  71.   edv = ifp->edv;
  72.   sendto(edv->fd, buf, l, 0, (struct sockaddr *) &edv->addr, sizeof(edv->addr));
  73.  
  74.   return l;
  75. }
  76.  
  77. /*---------------------------------------------------------------------------*/
  78.  
  79. static void ipip_recv(argp)
  80. void *argp;
  81. {
  82.  
  83.   char *bufptr;
  84.   char buf[MAX_FRAME];
  85.   int hdr_len;
  86.   int l;
  87.   struct edv_t *edv;
  88.   struct iface *ifp;
  89.   struct ip *ipptr;
  90.  
  91.   ifp = argp;
  92.   edv = ifp->edv;
  93.   l = recv(edv->fd, bufptr = buf, sizeof(buf), 0);
  94.   if (edv->type == IP_OVER_IP) {
  95.     if (l <= sizeof(struct ip )) goto fail;
  96.     ipptr = (struct ip *) bufptr;
  97.     hdr_len = 4 * ipptr->ip_hl;
  98.     bufptr += hdr_len;
  99.     l -= hdr_len;
  100.   }
  101.   if (l <= 0) goto fail;
  102.   net_route(ifp, ifp->type, qdata(bufptr, l));
  103.   return;
  104.  
  105. fail:
  106.   ifp->crcerrors++;
  107. }
  108.  
  109. /*---------------------------------------------------------------------------*/
  110.  
  111. int ipip_attach(argc, argv, p)
  112. int argc;
  113. char *argv[];
  114. void *p;
  115. {
  116.  
  117.   int fd;
  118.   int port;
  119.   int type;
  120.   int32 ipaddr;
  121.   struct edv_t *edv;
  122.   struct iface *ifp;
  123.   struct sockaddr_in addr;
  124.  
  125.   if (if_lookup(argv[1]) != NULLIF) {
  126.     printf("Interface %s already exists\n", argv[1]);
  127.     return (-1);
  128.   }
  129.  
  130.   switch (*argv[2]) {
  131.   case 'I':
  132.   case 'i':
  133.     type = IP_OVER_IP;
  134.     break;
  135.   case 'U':
  136.   case 'u':
  137.     type = IP_OVER_UDP;
  138.     break;
  139.   default:
  140.     printf("Type must be IP or UDP\n");
  141.     return (-1);
  142.   }
  143.  
  144.   if (!(ipaddr = resolve(argv[3]))) {
  145.     printf(Badhost, argv[3]);
  146.     return (-1);
  147.   }
  148.  
  149.   port = atoi(argv[4]);
  150.  
  151.   if (type == IP_OVER_IP)
  152.     fd = socket(AF_INET, SOCK_RAW, port);
  153.   else
  154.     fd = socket(AF_INET, SOCK_DGRAM, 0);
  155.   if (fd < 0) {
  156.     printf("cannot create socket: %s\n", sys_errlist[errno]);
  157.     return (-1);
  158.   }
  159.  
  160.   if (type == IP_OVER_UDP) {
  161.     memset(&addr, 0, sizeof(addr));
  162.     addr.sin_family = AF_INET;
  163.     addr.sin_addr.s_addr = INADDR_ANY;
  164.     addr.sin_port = htons(port);
  165.     if (bind(fd, &addr, sizeof(addr))) {
  166.       printf("cannot bind address: %s\n", sys_errlist[errno]);
  167.       return (-1);
  168.     }
  169.   }
  170.  
  171.   ifp = callocw(1, sizeof(*ifp));
  172.   ifp->name = strdup(argv[1]);
  173.   ifp->addr = Ip_addr;
  174.   ifp->mtu = MAX_FRAME;
  175.   setencap(ifp, "None");
  176.  
  177.   edv = calloc(1, sizeof(*edv));
  178.   edv->type = type;
  179.   edv->fd = fd;
  180.   edv->addr.sin_family = AF_INET;
  181.   edv->addr.sin_addr.s_addr = htonl(ipaddr);
  182.   edv->addr.sin_port = htons(port);
  183.   ifp->edv = edv;
  184.  
  185.   ifp->send = ipip_send;
  186.   on_read(fd, ipip_recv, (void * ) ifp);
  187.   ifp->next = Ifaces;
  188.   Ifaces = ifp;
  189.  
  190.   return 0;
  191. }
  192.  
  193.